package com.example.pixelyear.utils.dialog

import com.example.pixelyear.Interface.OnSelectColorListener
import com.example.pixelyear.R
import com.example.pixelyear.adapter.BaseAdapter
import com.example.pixelyear.adapter.ColorAdapter
import com.example.pixelyear.adapter.MoodAdapter
import com.example.pixelyear.bean.ColorBean
import com.example.pixelyear.sql.ColorSqlUtils
import com.example.pixelyear.ui.base.BaseActivity
import com.example.pixelyear.utils.UIUtils
import com.example.pixelyear.utils.extension.horizontalManager
import com.example.pixelyear.utils.extension.setOnDoubleClickListener
import com.example.pixelyear.utils.extension.verticalManager
import com.socks.library.KLog
import kotlinx.android.synthetic.main.dialog_add_date.view.*

class AddDateDialog(val ctx: BaseActivity) : BaseDialog(ctx) {
    private val mAdapter by lazy {
        ColorAdapter(ctx)
    }
    private val moodAdapter by lazy {
        MoodAdapter(ctx)
    }
    private var year = 0
    private var month = 0
    private var day = 0
    private var chooseColor = 0
    private var select: OnSelectColorListener? = null
    private val data = ColorBean(-1, "可以添加笔记哦～").also { it.image = R.mipmap.note }
    private val moodArray = UIUtils.getStringArray(R.array.mood)

    private var defaultMood = moodArray[0]

    private val colorSql by lazy {
        ColorSqlUtils(ctx)
    }

    override fun getLayoutID(): Int {
        return R.layout.dialog_add_date
    }

    override fun initAny() {
        rootView.Dialog_AddDate_RecyclerVertical.verticalManager()
        rootView.Dialog_AddDate_RecyclerVertical.adapter = mAdapter
        rootView.Dialog_AddDate_RecyclerHorizontal.horizontalManager()
        rootView.Dialog_AddDate_RecyclerHorizontal.adapter = moodAdapter
        moodAdapter.updateList(moodArray)
        KLog.e("yxs", "重新调用")

        if (mAdapter.isEmpty()) {
            mAdapter.add(data)
            mAdapter.addAll(colorSql.getData())
        }

        mAdapter.setOnItemClickListener(object : BaseAdapter.OnItemClick<ColorBean> {
            override fun click(bean: ColorBean, position: Int) {
                select?.OnSelect(year, month, day, bean.color, chooseColor, defaultMood)
                dismiss()
            }
        })

        moodAdapter.setOnItemClickListener(object : BaseAdapter.OnItemClick<String> {
            override fun click(bean: String, position: Int) {
                defaultMood = bean
            }
        })

        rootView.Dialog_AddDate_Edit.setOnDoubleClickListener {
            select?.OnEdit()
            dismiss()
        }
    }

    override fun windowHeight(): Float {
        return 0.7f
    }

    fun setOnSelectColorListener(listener: OnSelectColorListener) {
        select = listener
    }

    fun show(y: Int, m: Int, d: Int, c: Int) {
        year = y
        month = m
        day = d
        chooseColor = c
        super.show()
        rootView.Dialog_AddDate_Date.text = "$y - $m - $d"
    }

    fun setUpdate() {
        KLog.e("yxs", "触发了添加任务弹窗的列表更新")
        mAdapter.removeAll()
        mAdapter.add(data)
        mAdapter.addAll(colorSql.getData())
    }
}
